package com.cnb.telepathytalk.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cnb.telepathytalk.R;
import com.cnb.telepathytalk.dto.NoticeDTO;

public class NoticeListAdapter extends BaseAdapter {
	private ArrayList<NoticeDTO> list;
	private LayoutInflater inflater;
	private int layoutRsc;

	public NoticeListAdapter(Context context) {
		super();
		this.list = new ArrayList<NoticeDTO>();
		this.inflater = LayoutInflater.from(context);
		this.layoutRsc = R.layout.list_item_notice;
	}

	public ArrayList<NoticeDTO> getList() {
		return list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public NoticeDTO getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(layoutRsc, null);
			holder = new Holder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}

		NoticeDTO item = list.get(position);

		holder.getTitle().setText(item.getTitle());
		holder.getDate().setText(item.getDate());
		holder.getContent().setText(item.getContent());
		holder.getArrow().setSelected(item.isShown());
		holder.getBottomLayout().setVisibility(item.isShown() ? View.VISIBLE : View.GONE);

		return convertView;
	}

	public class Holder {
		private View baseView;
		private TextView title;
		private TextView date;
		private TextView content;
		private ImageView arrow;
		private View bottomLayout;

		public Holder(View view) {
			super();
			baseView = view;
		}

		public TextView getTitle() {
			if (title == null)
				title = (TextView) baseView.findViewById(R.id.notice_title);
			return title;
		}

		public TextView getDate() {
			if (date == null)
				date = (TextView) baseView.findViewById(R.id.notice_date);
			return date;
		}

		public TextView getContent() {
			if (content == null)
				content = (TextView) baseView.findViewById(R.id.notice_content);
			return content;
		}

		public ImageView getArrow() {
			if (arrow == null)
				arrow = (ImageView) baseView.findViewById(R.id.notice_arrow);
			return arrow;
		}

		public View getBottomLayout() {
			if (bottomLayout == null)
				bottomLayout = baseView.findViewById(R.id.bottom_layout);
			return bottomLayout;
		}
	}
}
